home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9422 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.8 KB

  1. Path: news.halcyon.com!usenet
  2. From: normanb@halcyon.com (Norm Bryar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Saving a C++ object
  5. Date: Fri, 01 Mar 1996 17:34:58 GMT
  6. Organization: Northwest Nexus Inc.
  7. Message-ID: <4h7cer$8pk@news.halcyon.com>
  8. References: <3135C74E.446B9B3D@doc.ic.ac.uk>
  9. NNTP-Posting-Host: blv-pm10-ip9.halcyon.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12.     Typically, a class has Save() and Load() methods that accept as an
  13. argument the archive into/from which you want to do the persistence.
  14. This allows you to "Save" to the clipboard, across a pipe, etc., as
  15. well as to a file.  
  16.  
  17.     Usually, classes write out their member variables item by item instead
  18. of saving themselves en-masse.  There are many reasons: first, the
  19. sizeof(ovoid) trick stores structure padding as well as data, so it's
  20. not readily portable or necessarily efficient.  
  21.     Second, if you ever do change the format of your class, you're clearly
  22. broken, whereas in the item-by-item approach, you can read a version
  23. number as the first item and steer the rest of your load operation
  24. accordingly.  
  25.     Third, you will probably want to call the Save() and Load() methods of
  26. your base-classes and your contained classes.  If you've contained
  27. objects by reference (pointer), you obviously don't want to save out
  28. the pointer, rather the pointed-to-object.  
  29.     Lastly, loading en-masse into a blob of memory, then type-casting the
  30. blob is constructing the object properly.  Virtual function tables
  31. will not be created, and virtual functions are the meat of OOP.
  32. Virtual inheritence will also be suspect.  
  33.  
  34.     Loading classes turns out to be tricky:  you've saved your drawing,
  35. let's say, as a Circle, a Square, and a Polygon.  When you go to load,
  36. all you know is that your drawing contains Shape classes, but you
  37. don't know which ones or in which order.  
  38.     Typically you make a special "factory" that knows how to load shapes.
  39. The Shape deriviatives save out a code that says "I'm a circle," then
  40. the factory loads this code and walks the list of registered classes
  41. until it finds the Circle class.  It instantiates the Circle, which
  42. set's it's "I'm a circle" code in the ctor, then the factory calls
  43. thecircle::Load() to finish loading the members.
  44.  
  45.     Hope this helps.
  46.                         --Norm 
  47.  
  48. Ben Jefferys <brj@doc.ic.ac.uk> wrote:
  49.  
  50. >When I save a C++ object just using:
  51.  
  52. >----------------------------
  53. >class ovoid{
  54. >/* etc. */
  55. >}
  56.  
  57. >ovoid square
  58.  
  59. >file.write((unsigned char *) &square, sizeof(ovoid))
  60. >----------------------------
  61.  
  62. >What *exactly* is saved? I'm finding that every time I recompile
  63. >an application when I've added some new code, trying to load
  64. >an object saved in this way with an old version of the application
  65. >crashes it, even if the class of the object in question hasn't
  66. >changed. ...
  67.  
  68. >Thanks for your help.
  69.  
  70. >-- 
  71. >Je suis triste et seul ici.
  72.  
  73.  
  74.